//
//  YSRechargeViewController.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-9-24.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSRechargeViewController.h"
#import "WXApi.h"
#import "WXApiObject.h"

@interface YSRechargeViewController () <UITextFieldDelegate, WXApiDelegate, UIAlertViewDelegate> {
    __weak IBOutlet UIScrollView *contentView;
    
    __weak IBOutlet UITextField *valueTextField;
}

@end

@implementation YSRechargeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(paySuccess)
                                                     name:kNotificationWeiXinPaySuccess
                                                   object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(payFail)
                                                     name:kNotificationWeiXinPayFail
                                                   object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)recharge:(id)sender {
    if (valueTextField.text.length == 0) {
        [self showMsgWithTitle:@"提示" msg:@"请输入要充值的金额"];
        return;
    }
    [YSProgressHUD showHUDAddedTo:self.view];
    [[NetworkMgr sharedManager] asynchRequestWithFunId:eAPI_RECHARGE
                                              delegate:self
                                                params:@{@"fee":valueTextField.text}];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [contentView setContentOffset:CGPointMake(0, 40) animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [contentView setContentOffset:CGPointMake(0, 0) animated:YES];
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return [YSUtility validateNumber:string];
}

#pragma mark NetWorkDelegate

- (void)didSuccess:(NetworkMgr *)api userInfo:(NSDictionary *)info {
    [YSProgressHUD hideHUDForView:self.view];
    NSInteger funId = [info[kFUN_ID] integerValue];
    NSDictionary *re = info[API_Result_RE];
    if ([YSUtility isBlankObject:re]) {
        return;
    }
    
    if (funId == eAPI_RECHARGE) {
        [self reqToWxPay:re];
    }
}


- (void)reqToWxPay:(NSDictionary *)info {
    
    PayReq *request = [[PayReq alloc] init];
    request.partnerId = WX_Partner_Id;
    request.prepayId = info[@"prepayid"];
    request.package = @"Sign=WXPay";
    request.nonceStr = info[@"noncestr"];
    request.timeStamp = [info[@"timestamp"] unsignedIntValue];
    request.sign = info[@"sign"];
    
    [WXApi safeSendReq:request];
}

- (void)paySuccess {
    [self showMsgWithTitle:@"提示" msg:@"充值成功" delegate:self tag:0];
}

- (void)payFail {
    [self showMsgWithTitle:@"提示" msg:@"充值失败"];
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        if (_blcok) {
            _blcok([valueTextField.text intValue] * 100);
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end
